package com.snts.synchronization.utils;

import java.io.PrintStream;
import java.util.ArrayList;

public class OutputFields {
	
	private ArrayList<Integer> fields = new ArrayList();
	
	public OutputFields() {
		super();
	}

	public void addField(int size){
		fields.add(size);
	}
	
	public Printer getPrinter(PrintStream printStream){
		return new Printer(printStream);
	}
	
	public class Printer {

		private int currentField = 0;
		private PrintStream printStream;
		
		public Printer(PrintStream printStream) {
			super();
			this.printStream = printStream;
		}

		public void print(String str){
			int fieldSize = fields.get(currentField);
			if(str.length() > fieldSize){
				str = str.substring(0, fieldSize);
			} else if(str.length() < fieldSize){
				StringBuffer buf = new StringBuffer("" + str);
				for(int i = str.length(); i < fieldSize; i++){
					buf.append(' ');
				}
				str = buf.toString();
			}
			printStream.print(str);
			currentField ++;
		}
		
		public void newLine(){
			printStream.println();
			currentField = 0;
		}
		
	}

}
